/*
 *所有关于cw_djlc_cwzb类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*cw_djlc_cwzbService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/

using VOL.Entity.DomainModels;
using VOL.Core.Utilities;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Finance.IRepositories;
using System.Collections.Generic;
using VOL.Core.DBManager;
using System;
using System.Linq;

namespace VOL.Finance.Services
{
    public partial class cw_djlc_cwzbService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Icw_djlc_cwzbRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public cw_djlc_cwzbService(
            Icw_djlc_cwzbRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        
        public override WebResponseContent Import(List<IFormFile> files)
        {
            WebResponseContent webResponse = new WebResponseContent();
            //导入保存前处理(可以对list设置新的值)

            ImportOnExecuting = (List<cw_djlc_cwzb> list) =>
            {
                /*foreach (cw_djlc_cwzb cwzb in list)
                {
                    if (cwzb!=null)
                    {
                        if (cwzb.artificial==null)
                        {
                            cwzb.artificial = 0;
                        }
                        if (cwzb.changepowercost == null)
                        {
                            cwzb.changepowercost = 0;
                        }
                        if (cwzb.changematerials == null)
                        {
                            cwzb.changematerials = 0;
                        }
                        if (cwzb.changemaintenance == null)
                        {
                            cwzb.changemaintenance = 0;
                        }
                        if (cwzb.salescommission == null )
                        {
                            cwzb.salescommission = 0;
                        }
                        if (cwzb.vehiclelogisticsfee == null)
                        {
                            cwzb.vehiclelogisticsfee = 0;
                        }
                        if (cwzb.sanbaoserviceFee == null)
                        {
                            cwzb.sanbaoserviceFee = 0;
                        }
                        if (cwzb.lianhetong == null)
                        {
                            cwzb.lianhetong = 0;
                        }
                    }
                }*/

                var dapper = DBServerProvider.SqlDapper;
                try
                {
                    dapper.BeginTrans();
                    string sql = "update cw_djlc_cwzb set status = 1 ";// 0 启用 ；1 禁用 
                    var data = dapper.ExcuteNonQuery(sql, new { });

                    dapper.Commit();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    dapper.Rollback();
                }
                return webResponse.OK();
            };
            return base.Import(files);
        }

        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            WebResponseContent webResponse = new WebResponseContent();
            AddOnExecuting = (cw_djlc_cwzb cwzb, object list) =>
            {
                /*if (cwzb != null)
                {
                    if (cwzb.artificial == null)
                    {
                        cwzb.artificial = 0;
                    }
                    if (cwzb.changepowercost == null)
                    {
                        cwzb.changepowercost = 0;
                    }
                    if (cwzb.changematerials == null)
                    {
                        cwzb.changematerials = 0;
                    }
                    if (cwzb.changemaintenance == null)
                    {
                        cwzb.changemaintenance = 0;
                    }
                    if (cwzb.salescommission == null)
                    {
                        cwzb.salescommission = 0;
                    }
                    if (cwzb.vehiclelogisticsfee == null)
                    {
                        cwzb.vehiclelogisticsfee = 0;
                    }
                    if (cwzb.sanbaoserviceFee == null)
                    {
                        cwzb.sanbaoserviceFee = 0;
                    }
                    if (cwzb.lianhetong == null)
                    {
                        cwzb.lianhetong = 0;
                    }
                }*/
                    return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }

        public override PageGridData<cw_djlc_cwzb> GetPageData(PageDataOptions options)
        {
            QueryRelativeExpression = (IQueryable<cw_djlc_cwzb> queryable) =>
            {
                //当前用户只能操作自己(与下级角色)创建的数据,如:查询、删除、修改等操作
                //IQueryable<int> userQuery = RoleContext.GetCurrentAllChildUser();
                //queryable = queryable.Where(x => x.CreateID == UserContext.Current.UserId || userQuery.Contains(x.CreateID ?? 0));
                queryable = queryable.Where(x => x.status == 0);
                return queryable;
            };
            return base.GetPageData(options);
        }

        public override WebResponseContent Update(SaveModel saveModel)
        {
            WebResponseContent webResponse = new WebResponseContent();
            UpdateOnExecuting = (cw_djlc_cwzb cwzb, object addList, object updateList, List<object> delKeys) =>
            {
                var vehiclelogisticsfee = cwzb.vehiclelogisticsfee;//获取整车物流费
                var cw_djlc_cwzb_id = cwzb.cw_djlc_cwzb_id;
               
                    string sql = "update cw_djlc_cwzb set [vehiclelogisticsfee] = @vehiclelogisticsfee where cw_djlc_cwzb_id = @cw_djlc_cwzb_id";
                    DBServerProvider.SqlDapper.ExcuteNonQuery(sql, new { vehiclelogisticsfee, cw_djlc_cwzb_id });
                    webResponse.Code = "-1";
                    return webResponse.OK("保存成功！");
              
            };
            return base.Update(saveModel);
        }
    }
}
